package com.lonely.fx.table_view;

import com.lonely.fx.table_view.entity.SimplePersonProperty;
import javafx.application.Application;
import javafx.beans.Observable;
import javafx.beans.binding.DoubleBinding;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.util.Callback;

/**
 * @date: 2022-08-02 11:03
 * @author: ztkj-hzb
 * @description: tableview 基于 propertyBean 基础属性设置
 **/
public class TableViewWithPropertyBeanSettingMain extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {


        AnchorPane anchorPane = new AnchorPane();

        HBox hBox = new HBox();

        Label nameLabel = new Label("修改name:");
        Label ageLabel = new Label("修改年龄：");

        TextField nameTf = new TextField();
        TextField ageTf = new TextField();

        hBox.getChildren().addAll(nameLabel, nameTf, ageLabel, ageTf);
        hBox.setSpacing(10);
        hBox.setAlignment(Pos.CENTER);
        hBox.prefWidthProperty().bind(anchorPane.widthProperty());


        //创建一个基于简单的javabean的tableview
        TableView<SimplePersonProperty> tableView = new TableView<>();

        //设置宽高
        tableView.setPrefSize(600, 400);

        //设置数据
        ObservableList<SimplePersonProperty> persons = FXCollections.observableArrayList(new Callback<SimplePersonProperty, Observable[]>() {
            @Override
            public Observable[] call(SimplePersonProperty param) {
                return new SimpleIntegerProperty[]{param.getAgeProperty()};
            }
        });

        persons.add(new SimplePersonProperty("小天", 20, "湖北省", false));
        persons.add(new SimplePersonProperty("小地", 30, "湖南省", true));
        persons.add(new SimplePersonProperty("小玄", 40, "广东省", false));
        persons.add(new SimplePersonProperty("小黄", 50, "浙江省", true));
        tableView.setItems(persons);

        //创建列
        TableColumn<SimplePersonProperty, String> nameTableCol = new TableColumn<>("用户名");
        TableColumn<SimplePersonProperty, Number> ageTableCol = new TableColumn<>("年龄");
        TableColumn<SimplePersonProperty, String> addressTableCol = new TableColumn<>("地址");
        TableColumn<SimplePersonProperty, Boolean> marriedTableCol = new TableColumn<>("是否已婚");
        tableView.getColumns().add(nameTableCol);
        tableView.getColumns().add(1, ageTableCol);
        tableView.getColumns().addAll(addressTableCol, marriedTableCol);

        //设置列与数据绑定

        //用PropertyValueFactory需要手动
        nameTableCol.setCellValueFactory(new PropertyValueFactory<>("name"));
        addressTableCol.setCellValueFactory(new PropertyValueFactory<>("address"));
        ageTableCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<SimplePersonProperty, Number>, ObservableValue<Number>>() {
            @Override
            public ObservableValue<Number> call(TableColumn.CellDataFeatures<SimplePersonProperty, Number> param) {
                return param.getValue().getAgeProperty();
            }
        });
        marriedTableCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<SimplePersonProperty, Boolean>, ObservableValue<Boolean>>() {
            @Override
            public ObservableValue<Boolean> call(TableColumn.CellDataFeatures<SimplePersonProperty, Boolean> param) {
                return param.getValue().getMarriedProperty();
            }
        });

        //宽度均分
        tableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);

        AnchorPane.setTopAnchor(hBox, 30.0);
        AnchorPane.setTopAnchor(tableView, 100.0);
        AnchorPane.setLeftAnchor(tableView, 100.0);
        anchorPane.getChildren().addAll(hBox, tableView);
        primaryStage.setScene(new Scene(anchorPane));
        primaryStage.setWidth(800);
        primaryStage.setHeight(600);
        primaryStage.show();


        nameTf.setOnKeyPressed(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent event) {
                if (event.getCode() != KeyCode.ENTER) {
                    return;
                }

                //TODO 修改name---无效，还是需要手动刷新
                persons.get(0).setName(nameTf.getText());
                //tableView.refresh();
            }
        });

        ageTf.setOnKeyPressed(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent event) {
                if (event.getCode() != KeyCode.ENTER) {
                    return;
                }

                //TODO 修改age---有效，直接刷新了
                persons.get(0).setAge(Integer.valueOf(ageTf.getText()));
                //tableView.refresh();
            }
        });


        persons.addListener(new ListChangeListener<SimplePersonProperty>() {
            @Override
            public void onChanged(Change<? extends SimplePersonProperty> c) {
                //要想监听触发到这里，需要在创建  FXCollections.observableArrayList 时指定CallBack
                if (c.next()) {
                    System.out.println(c.wasUpdated());
                }
            }
        });

    }
}
